<?php

namespace Common\Model;

/**
 * Description of DealerLoginModel
 * @index userlogin(`mobile`)
 */
class DealerLoginModel extends BaseModel {

    protected $trueTableName = 'tb_dealer_login';

    protected function _before_insert(&$data, $options) {
        // parent::_before_insert($data, $options);
    }

    protected function _before_update(&$data, $options) {
        // parent::_before_update($data, $options);
    }

    /**
     * 获取信息
     * @modify zhangzhen 2017-01-04 创建
     * @param  mixin   $dealerId        管理员id(s)
     * @return array              信息数组
     */
    public function getInfo($dealerId) {
        $option = array();

        if (is_array($dealerId)) {
            $dealerId = implode(',', $dealerId);
        }
        if (strpos($dealerId, ',') !== false) {
            $option['id'] = array('in', $dealerId);
            $data = $this->where($option)->select();
        } else {
            $option['id'] = $dealerId;
            $data = $this->where($option)->find();
        }

        return empty($data) ? array() : $data;
    }

    /**
     * 手机号返回用户数据
     * @modify zhangzhen 2017-1-4
     * @param string $mobile 手机号
     * @return mixed
     */
    public function getInfoByMobile($mobile, $areaCode = '86') {
        return $this->where(array('mobile' => $mobile,'areacode' => $areaCode))->find();
    }

    /**
     * 添加用户数据
     * @modify zhangzhen 2017-1-4
     * @param string $mobile 手机号
     * @param string $password 密码
     * @param string $name  用户名称
     * @return type
     */
    public function addInfo($mobile, $password, $name) {
        $options = array(
            'name' => $name,
            'mobile' => $mobile,
            'password' => $password
        );
        return $this->add($options);
    }

    /**
     * 重置用户密码
     * @modify zhangzhen 2017-1-4
     * @param string $userId  用户ID
     * @param string $password 重置密码
     * @return type
     */
    public function setPassword($userId, $password) {
        $result = $this->where(array('id' => $userId))->save(array('password' => $password));
        return $result === false ? false : true;
    }

    /**
     * 更新用户名称
     * @modify songanwei 2017-01-04
     * @param integer $userId 用户ID
     * @param string $name 用户名称
     * @return type
     */
    public function setName($userId, $name) {
        $result = $this->where(array('id' => $userId))->save(array('name' => $name));
        return $result === false ? false : true;
    }
    
    /**
     * 更换手机号
     * @modify zhangzhen 2017-01-04
     * @modify wenhui 2017-05-25 新增区号
     * @param integer $userId  用户ID
     * @param string $mobile  
     * @param string $areaCode  
     * @return type
     */
    public function setMobile($userId,$mobile,$areaCode = '86'){
        $result = $this->where(array('id'=>$userId))->save(array('mobile'=>$mobile));
        return $result === false ? false : true;
    }

}
